
class Node {
  constructor(element, next) {
    this.element = element
    this.next = next
  }
}

class Linked {
  constructor() {
    this.head = null
    this.size = 0
  }

  _getNode(index) {
    if(index < 0 || index >= this.size) {
      throw new Error('越界了')
    }
    let curNode = this.head
    for (let i = 0; i < index; i++) {
      curNode = curNode.next
    }
    return curNode
  }

  add(index, element) {
    // 参数个数
    if(arguments.length === 1) {
      element = index
      index = this.size
    }

    if (index < 0 || index > this.size) {
      throw new Error ('越界了')
    }

    // 添加
    if(index === 0) {
      let head = this.head
      this.head = new Node(element, head)
    } else {
      let prevNext = this._getNode(index - 1)
      prevNext.next = new Node(element, prevNext.next)
    }
    
    this.size++
  }

  remove(index) {
    let rmNode = null
    if(index === 0) {
      rmNode = this.head

      if(!rmNode) {
        return undefined
      }
      
      this.head = rmNode.next
    } else {
      let prevNext = this._getNode(index - 1)
      rmNode = prevNext.next
      prevNext.next = prevNext.next.next
    }
    this.size--
    return rmNode
  }

  set(index, element) {
    let node = this._getNode(index)
    node.element = element
  }

  get(index) {
    return this._getNode(index)
  }

  clear() {
    this.head = null
    this.size = 0
  }
}

class Queue {
  constructor() {
    this.linkList = new Linked()
  }

  enQueue(node) {
    this.linkList.add(node)
  }

  deQueue() {
    return this.linkList.remove(0)
  }
}

const q = new Queue()

q.enQueue('1')
q.enQueue('2')

let a = q.deQueue()
// a = q.deQueue()
// a = q.deQueue()

console.log(a);
